package com.bluesky.plum.uimodels.standard.components.objectBrowser.collectionEditor;

import java.util.List;

import com.bluesky.plum.richdomain.RichDomainField;
import com.bluesky.plum.uimodels.standard.StandardUIComponentType;
import com.bluesky.plum.uimodels.standard.UIComponent;
import com.bluesky.plum.uimodels.standard.UIComponentBuilder;
import com.bluesky.plum.uimodels.standard.components.Button;
import com.bluesky.plum.uimodels.standard.components.EventListener;
import com.bluesky.plum.uimodels.standard.components.containers.Panel;
import com.bluesky.plum.uimodels.standard.components.containers.form.commonForms.EntityEditDialogBuilder;
import com.bluesky.plum.uimodels.standard.components.containers.layout.VerticalBox;
import com.bluesky.plum.uimodels.standard.components.containers.window.Dialog;
import com.bluesky.plum.uimodels.standard.components.containers.window.ModalResult;
import com.bluesky.plum.uimodels.standard.components.containers.window.Window;
import com.bluesky.plum.uimodels.standard.components.listbox.SelectionMode;
import com.bluesky.plum.uimodels.standard.components.listbox.Selector;

/**
 * it is a very classical CRUD form dialog.<br>
 * on the top of the form is a table of all the objects(maybe paged), on the
 * below part of the form are 'add,modify,remove' buttons. click 'add or modfiy'
 * buttons will pop up a entity-edit-dialog. click on the remove button will
 * simply remove the selected object. <br>
 * the object list could be a simple ArrayList or a database-based repository
 * 
 * 
 * @author jack
 * 
 */
public class CollectionEditDialogBuilder {
	Dialog dialog;

	VerticalBox box;
	// then a table and 3 buttons

	Selector selector;
	Button addBtn;// =>saveBtn
	Button removeBtn;
	Button modifyBtn;// =>updateBtn
	Button returnBtn;// =>updateBtn

	RichDomainField rdf;
	protected UIComponentBuilder uiComponentBuilder;

	public CollectionEditDialogBuilder(final RichDomainField rdf,
			UIComponentBuilder uiComponentBuilder) {
		this.uiComponentBuilder = uiComponentBuilder;
		this.rdf = rdf;

		box = (VerticalBox) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.VerticalBox);

		box.setName("collection-edit-dialog-box");

		selector = (Selector) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Table);
		UIComponent selectionComponent = (UIComponent) selector;
		selector.setSelectionMode(SelectionMode.Single);
		selector.setObjects((List) rdf.value);

		addBtn = (Button) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Button);

		addBtn.setCaption("Add");

		addBtn.setClickListener(new EventListener() {
			@Override
			public void process() {
				// System.out.println("add");
				final Window window = dialog.getWindow();

				Object newElementInstance = null;
				try {
					newElementInstance = getRdf().elementClass.newInstance();

				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				EntityEditDialogBuilder dialogBuilder = new EntityEditDialogBuilder(
						newElementInstance, dialog.getUiComponentBuilder());

				Dialog dialog = dialogBuilder.getDialog(window,
						"entity edit dialog");

				window.getRootWindow().repaint();

				dialog.setModal(true);
				dialog.pack();
				dialog.setVisible(true);

				if (dialog.getModalResult() == ModalResult.OK) {
					((List) rdf.value).add(newElementInstance);
					selector.setObjects((List) rdf.value);
				}

				window.removeChildWindow(dialog);

				System.out.println("exit from entity edit dialog");

			}
		});

		removeBtn = (Button) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Button);

		removeBtn.setCaption("Remove");

		removeBtn.setClickListener(new EventListener() {
			@Override
			public void process() {
				System.out.println("remove");

				((List) rdf.value).remove(selector.getSelectedIndex());
				selector.setObjects((List) rdf.value);
			}
		});

		modifyBtn = (Button) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Button);

		modifyBtn.setCaption("Modify");

		modifyBtn.setClickListener(new EventListener() {
			@Override
			public void process() {
				System.out.println("modify");

				/*
				 * to modify an entity also will pop up a entity-edit-dialog
				 */
				final Window window = dialog.getWindow();

				Object newElementInstance = selector.getSelectedValue();
				if(newElementInstance==null)
					try {
						newElementInstance=rdf.elementClass.newInstance();
					} catch (Exception e) {
						
						e.printStackTrace();
						throw new RuntimeException(e);
					} ;

				EntityEditDialogBuilder dialogBuilder = new EntityEditDialogBuilder(
						newElementInstance, dialog.getUiComponentBuilder());

				Dialog dialog = dialogBuilder.getDialog(window,
						"collection edit dialog");

				window.getRootWindow().repaint();

				dialog.setModal(true);
				dialog.pack();
				dialog.setVisible(true);

				if (dialog.getModalResult() == ModalResult.OK) {

					selector.setObjects((List) rdf.value);
				}

			}
		});

		returnBtn = (Button) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Button);

		returnBtn.setCaption("Return");

		returnBtn.setClickListener(new EventListener() {
			@Override
			public void process() {
				System.out.println("return");

				//modalResult = true;

				// return
				dialog.setVisible(false);

			}
		});

		box.addChildComponent(selectionComponent);
		box.addChildComponent(addBtn);
		box.addChildComponent(removeBtn);
		box.addChildComponent(modifyBtn);
		box.addChildComponent(returnBtn);

		// return box;

	}

	public Dialog getDialog(Window parent, String title) {
		dialog = (Dialog) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Dialog);

		// create a dialog
		parent.addChildWindow(dialog);

		dialog.setTitle(title);

		final Panel contentPanel = (Panel) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Panel);
		dialog.setContentPanel(contentPanel);

		contentPanel.addChildComponent(box);
		dialog.setContentPanel(contentPanel);

		return dialog;
	}

	public Selector getSelector() {
		return this.selector;
	}

	public Dialog getDialog() {
		return dialog;
	}

	public void setDialog(Dialog dialog) {
		this.dialog = dialog;
	}

	public RichDomainField getRdf() {
		return rdf;
	}

	public void setRdf(RichDomainField rdf) {
		this.rdf = rdf;
	}

}
